# -*- codeing = utf-8 -*-
# @Time : 2021/3/2 11:22
# @Atuthor: 朱朱
# @File: files_manage.py
# @Software: PyCharm
""

"""
主要是读取数据文件
"""
BASE_URL = './files/'


def read_text(file_name):
    try:
        with open(BASE_URL + file_name, 'r', encoding='utf8') as file:
            content = file.read()
        return content
    except FileNotFoundError:
        print('文件未找到')


def read_json(json_name, non_data=None):  # 如果没有找到会返回一个空字典non_data
    if non_data is None:
        non_data = {}
    try:
        with open(BASE_URL + json_name, 'r', encoding='utf8') as file:
            import json
            j_content = json.load(file)  # load: 读取 '文件' ，把读取的内容加载成为Python里的数据
        return j_content
    except FileNotFoundError:
        return non_data


def write_json(json_name, data):
    with open(BASE_URL + json_name, 'w', encoding='utf8') as file:
        import json
        json.dump(data, file) # 将数据转换为字典,并写入文件
        # 写成功,不需要返回值

# print(read_text('students_page.txt'))
#
# print(read_json('teacher.json'))
#
# print(write_json('jack','hahahah'))
